package com.ayf.payment.game.api.controller.platform;


import com.ayf.payment.game.api.dto.ChannelTradeAmountRandomRuleDTO;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.ChannelTradeAmountRandomRuleService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import com.ayf.payment.game.api.utils.SnowFlakeUtils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/t/channel/trade/rule")
@RestController
public class ChannelTradeAmountRandomRuleController {

    @Autowired
    private ChannelTradeAmountRandomRuleService channelTradeAmountRandomRuleService;

    /**
     * 获取通道交易随机数规则列表
     * @return
     */
    @PostMapping("/list")
    public CommonResult<List<ChannelTradeAmountRandomRuleDTO>> getChannelTradeAmountRandomRuleList(@RequestBody ChannelTradeAmountRandomRuleDTO channelTradeAmountRandomRuleDTO) {
        List<ChannelTradeAmountRandomRuleDTO> list=channelTradeAmountRandomRuleService.getChannelTradeAmountRandomRuleList(channelTradeAmountRandomRuleDTO);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", list);
    }

    /**
     * 保存渠道交易随机数规则
     * @param channelTradeAmountRandomRuleDTO
     * @return
     */
    @PostMapping("/saveAndUpChannelTradeAmtRule")
    public CommonResult saveAndUpChannelTradeAmtRule(@RequestBody ChannelTradeAmountRandomRuleDTO channelTradeAmountRandomRuleDTO) {
        if(channelTradeAmountRandomRuleDTO==null){
            throw new ParameterException("参数不能为空");
        }
        try {
            channelTradeAmountRandomRuleService.saveAndUpChannelTradeAmtRule(channelTradeAmountRandomRuleDTO);
        } catch (org.springframework.dao.DuplicateKeyException e) {
            return new CommonResult<>(-5, "判断交易金额区间不允许重复", "");
        }catch (Exception e) {
            String traceId = SnowFlakeUtils.getTraceId();
            LogPortal.error("{} 尾数金额保存失败",e,traceId);
            return new CommonResult<>(-5, "尾数金额保存失败，错误码:"+traceId, "");
        }
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "更新成功", "");
    }

    @PostMapping("/deleteChannelTradeAmtRule")
    public CommonResult deleteChannelTradeAmtRule(@RequestBody ChannelTradeAmountRandomRuleDTO channelTradeAmountRandomRuleDTO) {
        if(channelTradeAmountRandomRuleDTO==null || channelTradeAmountRandomRuleDTO.getId() == null){
            throw new ParameterException("参数不能为空");
        }
        channelTradeAmountRandomRuleService.deleteChannelTradeAmtRuleById(channelTradeAmountRandomRuleDTO.getId());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "删除成功", "");
    }
}
